package cz.proks.easyhelpdesk.gae.api.impl.dbo;

import org.springframework.beans.BeanUtils;

import cz.proks.easyhelpdesk.api.model.Category;
import cz.proks.easyhelpdesk.gae.api.impl.dbo.base.ConvertibleDbo;
import cz.proks.easyhelpdesk.gae.api.impl.dbo.base.IdentifiableModelDbo;



public class Ticket extends cz.proks.easyhelpdesk.api.model.Ticket implements IdentifiableModelDbo, ConvertibleDbo<cz.proks.easyhelpdesk.api.model.Ticket> {        
    private User owner;
    private Category category;     
    private boolean isThreadFirst = false;
    
    @Override
    public Long getOwnerId() {     
        return super.getOwnerId();
    }

    @Override
    public void setOwnerId(Long ownerId) {
        super.setOwnerId(ownerId);
        
        if (ownerId == null)
            owner = null;
        else { 
            owner = new User();
            owner.setId(ownerId);
        }        
    }  
    
    @Override
    public Long getCategoryId() {    
        return super.getCategoryId();
    }

    @Override
    public void setCategoryId(Long categoryId) {        
        super.setCategoryId(categoryId);
        
        if (categoryId == null)
            category = null;
        else {
            category = new Category();
            category.setId(categoryId);
        }
    }
        
    public boolean isThreadFirst() {
        return isThreadFirst;
    }

    public void setThreadFirst(boolean isHeading) {
        this.isThreadFirst = isHeading;
    }
                   
    public User getOwner() {
        return owner;
    }

    public void setOwner(User owner) {
        this.owner = owner;
        super.setOwnerId(owner != null ? owner.getId() : null);
    }        
      
    public Category getCategory() {
        return category;
    }

    public void setCategory(Category category) {
        this.category = category;
        super.setCategoryId(category != null ? category.getId() : null);
    }    

    public void toDbo(cz.proks.easyhelpdesk.api.model.Ticket ticket) {
        BeanUtils.copyProperties(ticket, this);
    }
    
    public cz.proks.easyhelpdesk.api.model.Ticket asDomain() {
        cz.proks.easyhelpdesk.api.model.Ticket ticket = new cz.proks.easyhelpdesk.api.model.Ticket();
        BeanUtils.copyProperties(this, ticket, new String[] {"helpdesk", "owner", "solver", "multipartFiles", "isThreadFirst"});        
        return ticket;
    }
}
